package ru.niir.goodfil.ui;

import ru.niir.goodfil.db.Order;
import ru.niir.goodfil.db.dic.OrdersReportType;
import ru.niir.goodfil.utils.SumFormat;
import ru.niir.goodfil.utils.filters.OrdersFilter;

import javax.faces.model.ListDataModel;
import java.util.*;

public class StoreReportBean extends PageBean {

    private Date beginDate;

    private Date endDate;

    /**
     * Счетчики
     */
    private int prepareInTimeOrdersCount;

    private int notPrepareInTimeOrdersCount;

    private int deliveryInTimeOrdersCount;

    private int notDeliveryInTimeOrdersCount;

    private int recivedOrdersCount;

    private List<Order> orderList = new ArrayList<Order>();

    private int reportType;

    private String volumeOfExportedGoods;

    public StoreReportBean() {
        init();
    }

    public void init(){
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        beginDate = calendar.getTime();
        endDate = new Date();
        orderList = getOrders();
        setVolumeOfExportedGoods(getVolumeOfExportedGoods(getBeginDate(), getEndDate()));
        initialize();
    }

    public void initialize(){
        List<Order> list = gf().getOrdersByDeliveryDate(beginDate, endDate);
        for(Order order : list){
            if(order.getOrderIsPrepareInTime()){
                prepareInTimeOrdersCount++;
            }
            else {
                notPrepareInTimeOrdersCount++;
            }

            if(order.getOrderIsDeliveryInTime()){
                deliveryInTimeOrdersCount++;
            }

            else{
                notDeliveryInTimeOrdersCount++;
            }
            recivedOrdersCount++;
        }


    }

    private List<Order> getOrders() {
        List<Order> list = gf().getOrdersByDeliveryDate(beginDate, endDate);
        orderList.clear();
        if(reportType == OrdersReportType.PREPARE_IN_TIME){
            for(Order order : list){
                if(order.getOrderIsPrepareInTime()){
                    orderList.add(order);
                }
                continue;
            }
        }

        else if(reportType == OrdersReportType.NOT_PREPARE_IN_TIME){
            for(Order order : list){
                if(!order.getOrderIsPrepareInTime()){
                    orderList.add(order);
                }
                continue;
            }
        }


        else if(reportType == OrdersReportType.DELIVERY_IN_TIME){
            for(Order order : list){
                if(order.getOrderIsDeliveryInTime()){
                    orderList.add(order);
                }
                continue;
            }
        }

        else if(reportType == OrdersReportType.NOT_DELIVERY_IN_TIME){
            for(Order order : list){
                if(!order.getOrderIsDeliveryInTime()){
                    orderList.add(order);
                }
                continue;
            }
        }

        return orderList;
    }

    public void reloadData(){
        setBeginDate(beginDate);
        setEndDate(endDate);
        recivedOrdersCount = 0;
        prepareInTimeOrdersCount = 0;
        notPrepareInTimeOrdersCount = 0;
        deliveryInTimeOrdersCount = 0;
        notDeliveryInTimeOrdersCount = 0;
        orderList = getOrders();
        setVolumeOfExportedGoods(getVolumeOfExportedGoods(getBeginDate(), getEndDate()));
        initialize();
    }

    public Date getBeginDate() {
        if(beginDate == null){
            return null;
        }
        else return new Date(beginDate.getTime());
    }

    public void setBeginDate(Date beginDate) {
        if (beginDate != null) {
            this.beginDate = beginDate;
        } else {
            this.beginDate = null;
        }
    }

    public Date getEndDate() {
        if(endDate == null){
            return null;
        }
        else return new Date(endDate.getTime());
    }

    public void setEndDate(Date endDate) {
        if (endDate != null) {
            this.endDate = endDate;
        } else {
            this.endDate = null;
        }
    }

    private String getVolumeOfExportedGoods(Date beginDate, Date endDate){
        return SumFormat.formatSum("###,###,##0.00", gf().getVolumeOfExportedGoods(beginDate, endDate));
    }

    public int getNotPrepareInTimeOrdersCount() {
        return notPrepareInTimeOrdersCount;
    }

    public int getPrepareInTimeOrdersCount() {
        return prepareInTimeOrdersCount;
    }

    public int getRecivedOrdersCount() {
        return recivedOrdersCount;
    }

    public int getDeliveryInTimeOrdersCount() {
        return deliveryInTimeOrdersCount;
    }

    public int getNotDeliveryInTimeOrdersCount() {
        return notDeliveryInTimeOrdersCount;
    }

    public int getReportType() {
        return reportType;
    }

    public void setReportType(int reportType) {
        this.reportType = reportType;
    }

    public List<Order> getOrderList() {
        return orderList;
    }

    public String getReportTypeAsString(){
        return OrdersReportType.asString(reportType);
    }

    public String getVolumeOfExportedGoods() {
        return volumeOfExportedGoods;
    }

    public void setVolumeOfExportedGoods(String volumeOfExportedGoods) {
        this.volumeOfExportedGoods = volumeOfExportedGoods;
    }



    public String getOrdersSum(){
        double sum = 0;
        for(Order order : orderList){
            if(order.getFactSum() != 0){
                sum += order.getFactSum();
                continue;
            }
            sum += order.getSum();
        }
        return SumFormat.formatSum("###,###,##0.00", sum);
    }

}
